﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NHibernate.Criterion;
using ZDSoft.QLMS.Component;
using ZDSoft.QLMS.Web.Helpers;

namespace ZDSoft.QLMS.Web.Controllers
{
    public class TypeController : ControllerBase
    {
        //
        // GET: /Type/
        TypeComponent typeComponet = new TypeComponent();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Create()
        {
            return View();
        }

        public void TypeCreate(string Name, string IsActive)
        {
            Entity.Type types = new Entity.Type();
            types.Name = Name;
            types.IsActive = bool.Parse(IsActive);
            types.Create();
        }
        public ActionResult Update()
        {
            return View();
        }
        public void TypeUpdate(int id, string Name, string IsActive)
        {
            Entity.Type types = typeComponet.FindByID(id);
            types.Name = Name;
            types.IsActive = bool.Parse(IsActive);
            types.Update();
        }
        public string GetUserByID(int id)
        {
            Entity.Type type = typeComponet.FindByID(id);
            IList<KeyValuePair<string, InvokeMethod>> list = new List<KeyValuePair<string, InvokeMethod>>();
            string str = type.ToJsonString<Entity.Type>(list);
            return str;
        }
        /// <summary>
        /// 修改状态
        /// </summary>
        [HttpPost]
        public void Withdraw(int id)
        {
            Entity.Type entity = typeComponet.FindByID(id);
            if (entity.IsActive == true)
            {
                entity.IsActive = false;
            }
            else
            {
                entity.IsActive = true;
            }
            entity.Update();
        }
        /// <summary>
        /// 分页，排序
        /// </summary>
        public JsonResult GetPaged(int pageIndex, int pageSize, string strSearchProperty,
            string strSearchCondition, string strSortProperty, string strSortCondition)
        {
            IList<ICriterion> queryConditions = new List<ICriterion>();
            IList<Order> orderList = new List<Order>();
            if (strSearchProperty != "")
            {
                string[] searchProperties = strSearchProperty.Split(',');
                string[] searchConditions = strSearchCondition.Split(',');
                queryConditions = new List<ICriterion>();
                for (int i = 0; i < searchProperties.Length; i++)
                {
                    queryConditions.Add(Expression.Eq(searchProperties[i], searchConditions[i]));
                }
            }
            if (strSortProperty != "")
            {
                string[] sortProperties = strSortProperty.Split(',');
                string[] sortConditions = strSortCondition.Split(',');
                orderList = new List<Order>();
                for (int i = 0; i < sortProperties.Length; i++)
                {
                    if (sortConditions[i] == "asc")
                    {
                        orderList.Add(new Order(sortProperties[i], true));
                    }
                    else
                    {
                        orderList.Add(new Order(sortProperties[i], false));
                    }
                }
            }

            int count;
            int pageNum;

            IList<Entity.Type> list = new List<Entity.Type>();
            IList<Entity.Type> typelist = typeComponet.GetPaged(queryConditions, orderList, pageIndex, pageSize, out count);

            int tmpcountI = count / pageSize;
            float temCountF = float.Parse(count.ToString()) / float.Parse(pageSize.ToString());
            if (temCountF > float.Parse(tmpcountI.ToString()))
            {
                pageNum = tmpcountI + 1;
            }
            else
            {
                pageNum = tmpcountI;
            }

            foreach (Entity.Type types in typelist)
            {
                types.ToJaonResult<Entity.Type>();
                list.Add(types);
            }
            list.Add(new Entity.Type() { ID = pageNum });
            JsonResult result = this.Json(list, JsonRequestBehavior.AllowGet);
            return result;
        }

    }
}
